package com.koron.css2.workflow.variables;

import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工程报装程审批变量
 *
 * @author admit
 * @version 2.0
 * @since 2021.10.14
 */
@Service("pj_info_new")
public class PjInfoNewWorkflowVariable implements WorkflowVariable {

    private static Logger logger = LoggerFactory.getLogger(PjInfoNewWorkflowVariable.class);

    @Override
    public Map<String, Object> getVariable(SessionFactory factory, Map<String, Object> variable, String biillId, UserInfoBean userInfo) {
        if (variable == null) {
            variable = new HashMap<String, Object>();
        }
        // 1. 查出对应的工程报装单
        PjInfoBean pjInfo = factory.getMapper(PjInfoMapper.class).selectById(biillId);
        if (pjInfo == null){
            return variable;
        }
        // 营业区域
        String businessArea = pjInfo.getBusinessArea();
        if (StringUtils.isNotBlank(businessArea)) {
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
            String businessAreaName = businessAreaMapper.findBusinessAreaById(businessArea, userInfo.getCurWaterCode());
            variable.put("businessArea", businessAreaName);
        }
        // 2. 勘察邀约候选人列表
        String surveyStaff = pjInfo.getSurveyStaff();
        if (StringUtils.isNotBlank(surveyStaff)) {
            OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class);
            OrgUserQueryBean query = new OrgUserQueryBean();
            query.setUserAccounts(Arrays.asList(surveyStaff.split(",", -1)));
            List<OrgUserBean> orgUsers = mapper.selectByAccounts(query);
            surveyStaff = orgUsers.stream().map(t -> t.getCode()).collect(Collectors.joining(","));
            variable.put("inviteUserList", surveyStaff);
            variable.put("survey", surveyStaff);
        }
        // 3. 工程验收候选人列表
        String checkInviteStaff = pjInfo.getCheckInviteStaff();
        if (StringUtils.isNotBlank(checkInviteStaff)) {
            OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class);
            OrgUserQueryBean query = new OrgUserQueryBean();
            query.setUserAccounts(Arrays.asList(checkInviteStaff.split(",", -1)));
            List<OrgUserBean> orgUsers = mapper.selectByAccounts(query);
            checkInviteStaff = orgUsers.stream().map(t -> t.getCode()).collect(Collectors.joining(","));
            variable.put("checkInviteUserList", checkInviteStaff);
        }
        String meterBore = pjInfo.getMeterBore();
        if (StringUtils.isNotBlank(meterBore)) {
            MeterBoreMapper boreMapper = factory.getMapper(MeterBoreMapper.class);
            MeterBoreBean bore = boreMapper.selectMeterBoreById(meterBore);
            variable.put("bore", bore.getBoreValue());
        }else {
            variable.put("bore", "1");
        }
        Double budgetMoney = pjInfo.getBudgetMoney();
        if (budgetMoney != null){
            variable.put("budgetMoney", budgetMoney);
        }
        Integer redLineConstruction = pjInfo.getRedLineConstruction();
        if (redLineConstruction != null){
            variable.put("redLine", redLineConstruction);
        }
        String constGroup = pjInfo.getConstGroup();
        if (constGroup != null){
            variable.put("installBuild", constGroup);
            variable.put("build", constGroup);
        }
        String surveyInviteStaff = pjInfo.getSurveyInviteStaff();
        if (surveyInviteStaff != null){
            variable.put("invitationSurvey", surveyInviteStaff);
        }
        return variable;
    }
}
